﻿using UnityEngine;
using Zero;

/// <summary>
/// 元素反应：扩散
/// </summary>
public class ElementSwirl : IElementReaction
{
    private readonly IElementHolder _holder;
    private readonly ElementSwirlState _state;

    public ElementSwirl(IElementHolder holder, ElementSwirlState state)
    {
        _holder = holder;
        _state = state;
        _state.Start(_holder.Owner);
    }

    public void Reaction(TriggerElementData triggerElementData)
    {
        // 扩散不消耗风元素量
        if (triggerElementData.ElementType != ElementType.Wind || triggerElementData.Gauge <= 0f)
        {
            return;
        }

        TrySwirl(ElementType.Fire, triggerElementData);
        TrySwirl(ElementType.Water, triggerElementData);
        TrySwirl(ElementType.Electric, triggerElementData);
        TrySwirl(ElementType.Ice, triggerElementData);

        if (triggerElementData.IsTriggered)
        {
            ServiceLocator.Get<IWorldNumberService>().CreateElementReaction(_holder.Owner.transform.position, "扩散");
        }
    }

    private void TrySwirl(ElementType elementType, TriggerElementData triggerElementData)
    {
        var aura = _holder.GetGeneralElementAura(elementType);
        if (aura.Gauge <= 0f)
        {
            return;
        }

        triggerElementData.IsTriggered = true;

        var reactionGauge = aura.Gauge >= 0.5f * triggerElementData.Gauge ? triggerElementData.Gauge : aura.Gauge;
        Debug.Log($"触发元素量: {triggerElementData.ElementType} | {triggerElementData.Gauge}");
        Debug.Log($"当前元素量: {elementType} | {aura.Gauge}");
        Debug.Log($"反应消耗元素量: {reactionGauge}");
        // 消耗附着元素
        aura.UseGauge(triggerElementData.Gauge * 0.5f);
        Debug.Log($"剩余元素量: {elementType} | {aura.Gauge}");
        _state.AddSwirl(elementType, (reactionGauge - 0.04f) * 1.25f + 1f);
    }
}